package cn.aicnn.chatssespringboot.service.Impl;

import cn.aicnn.chatssespringboot.dto.Account;
import cn.aicnn.chatssespringboot.dto.User;
import cn.aicnn.chatssespringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.aicnn.chatssespringboot.persistence.userMapper;
import cn.aicnn.chatssespringboot.persistence.accountMapper;

@Service
public class UserServicelmpl implements UserService {
    @Autowired
    private userMapper userMapper;

    @Autowired
    private accountMapper accountMapper;

    @Override
    public User login(String username, String password) {
        // 调用UserMapper中的方法查询用户信息
        User user = userMapper.findByUsernameAndPassword(username, password);

        return user;
    }

    @Override
    public boolean register(User user) {
        User existingUser = userMapper.selectByUsername(user.getUsername());
        if (existingUser != null) {
            return false; // 用户名已存在，注册失败
        }

        Account account = new Account();
        account.setUsername(user.getUsername());
        account.setPassword(user.getPassword());
        account.setPhone("1"); // 设置默认值为 1
        account.setFirstname("1"); // 设置默认值为 1
        account.setLastname("1"); // 设置默认值为 1
        account.setQuestion(account.getQuestion());

        int rowsAffectedAccount = accountMapper.insert(account);
        int rowsAffected = userMapper.insert(user);
        return rowsAffected > 0; // 注册成功返回true，失败返回false
    }


}
